<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html lang="en">
<head>
	<title>Latin Nouns</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="../classics.css" rel="stylesheet" type="text/css">
</head>

<?php
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}


$endings = array('Singular' => array('Nom.' => '', 'Gen.' => 'ī', 'Dat.' => 'ō', 'Acc.' => 'um', 'Abl.' => 'ō', 'Voc.' => 'e'),
	             'Plural' => array('Nom.' => 'ī', 'Gen.' => 'ōrum', 'Dat.' => 'īs', 'Acc.' => 'ōs', 'Abl.' => 'īs'));

$nouns = array('servus' => 'serv', 'bellum' => 'bell', 
               'ager' => 'agr', 'puer' => 'puer', 'vir' => 'vir',
               'annus' => 'ann', 'amīcus' => 'amīc', 'dominus' => 'domin',
               'gener' => 'gener', 'socer' => 'socer', 'lūcifer' => 'lūcifer', 'līber' => 'līber',
               'magister' => 'magistr', 'cancer' => 'cancr', 'liber' => 'libr',
               'regnum' => 'regn', 'verbum' => 'verb');

$Nom = array_rand($nouns);
$stem = $nouns[$Nom];

$vtab = "<caption>".$Nom."</caption>\n";
$vtab = $vtab . "<tr><td></td><th>Singular</th><th>Plural</th></tr>\n";

foreach(array('Nom.', 'Gen.', 'Dat.', 'Acc.', 'Abl.', 'Voc.') as $case) {
	$vtab = $vtab . "<tr><th>$case</th>";
	foreach(array('Singular', 'Plural') as $number) {
		$vtab = $vtab . "<td>";
		if ($Nom == 'līber' && $number == 'Singular') {
			
		} elseif (endsWith($Nom, 'um') && $number == 'Plural' && ($case == 'Nom.' || $case == 'Acc.')) {
			$txts[] = $stem."a";
			$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
		} elseif ($case == 'Nom.' && $number == 'Singular') {
			$txts[] = $Nom;
			$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
		} elseif ($case == 'Voc.') {
			if ($number == 'Singular' && endsWith($Nom, 'us')) {
				$txts[] = $stem.$endings[$number][$case];
				$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
			}
		} elseif (isset($endings[$number][$case])) {
			$txts[] = $stem.$endings[$number][$case];
			$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
		}
		$vtab = $vtab . "</td>";
	}
	$vtab = $vtab . "</tr>\n";
}
?>

<script type="text/javascript">
function check() 
{

<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "if (document.n01.text".$i.".value=='".$txts[$i]."')\n{\n";
		print "\tdocument.n01.text".$i.".disabled=true;\n}\n";
	}
?>

}

function newWord() 
{
<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "document.n01.text".$i.".disabled=false;\n";
 		print "document.n01.text".$i.".value='';\n";
	}
?>
	location.reload();
}
</script>

<body>
<div id="container">
<div id="center">
<form name="n01" action="">
<table align="center">
<?php
	print $vtab;
?>
<tr><td></td>
	<td align="center"><input name="Check" onclick="check()" value="Check" type="button"></td>
	<td align="center"><input name="New Word" onclick="newWord()" value="New Word" type="button"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
